﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using CE.Global;

public partial class controls_usrDOB : System.Web.UI.UserControl
{
    Int32 intDay = -1;
    Int32 intMonth = -1;
    Int32 intYear = -1;
    Int32 intStartYear = General.GetUTCDateTime().Year - 50;
    Int32 intEndYear = General.GetUTCDateTime().Year + 50;
    string strValidationGroup = "";
    bool bEnabled = true;
    bool bReadOnly = false;
    bool bAllowGreaterThanToday = true;
    bool bIsTravelInsurance = true;
    bool bIsNotLessThanToday = true;
    string strErrorMessage = "Choose a date.";
    //string strMonthErrorMessage = "Choose Month.";
    //string strYearErrorMessage = "Choose Year.";

    protected void Page_Load(object sender, EventArgs e)
    {
        //string name = this.ClientID;
        //string[] elm = ctrlUserPrefix.ClientID.Split('_');		//get an array of values 
        //ctrlUserPrefix.Text = ctrlUserPrefix.ClientID.Replace(elm[elm.Length - 1], "");    //get the last element and replace from the id     
        if (!Page.IsPostBack)
        {
            if (intStartYear == 0) intStartYear = General.GetUTCDateTime().Year - 75;
            if (intEndYear == 0) intEndYear = General.GetUTCDateTime().Year + 20;

            ddlYear.Items.Clear();

            for (int i = intEndYear; i >= intStartYear; i--)
            {
                if (CultureInfo.CurrentCulture.Name != "th-TH")
                {
                    ddlYear.Items.Add(i.ToString());
                }
                else
                {
                    ddlYear.Items.Add(new ListItem((i + 543).ToString(), i.ToString()));
                }
            }

            ddlYear.Items.Insert(0, new ListItem(Resources.CarInsurance.Year, "-1"));

            ddlDay.Attributes.Add("onchange", "ShowValidationEffectTwo('" + ddlDay.ClientID + "','" + rfvDay.ClientID + "','" + csvDate.ClientID + "');");
            ddlMonth.Attributes.Add("onchange", "ShowValidationEffectTwo('" + ddlMonth.ClientID + "','" + rfvMonth.ClientID + "','" + csvDate.ClientID + "');");
            ddlYear.Attributes.Add("onchange", "ShowValidationEffectTwo('" + ddlYear.ClientID + "','" + rfvYear.ClientID + "','" + csvDate.ClientID + "');");
        }
    }

    public void ChangeValidationEffect()
    {
        ddlDay.ControlStyle.CssClass = "validationError";
        ddlMonth.ControlStyle.CssClass = "validationError";
        ddlYear.ControlStyle.CssClass = "validationError";
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvDay.Enabled && rfvDay.ValidationGroup == strValidationGroup)
        {
            rfvDay.Validate();
            if (rfvDay.IsValid == false)
            {
                ddlDay.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlDay.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlDay.ControlStyle.CssClass = "";
        }

        if (rfvMonth.Enabled && rfvMonth.ValidationGroup == strValidationGroup)
        {
            rfvMonth.Validate();
            if (rfvMonth.IsValid == false)
            {
                ddlMonth.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlMonth.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlMonth.ControlStyle.CssClass = "";
        }

        if (rfvYear.Enabled && rfvYear.ValidationGroup == strValidationGroup)
        {
            rfvYear.Validate();
            if (rfvYear.IsValid == false)
            {
                ddlYear.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlYear.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlYear.ControlStyle.CssClass = "";
        }

        if (csvDate.Enabled && csvDate.ValidationGroup == strValidationGroup)
        {
            csvDate.Validate();
            if (csvDate.IsValid == false)
            {
                ddlDay.ControlStyle.CssClass = "validationError";
                ddlMonth.ControlStyle.CssClass = "validationError";
                ddlYear.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlDay.ControlStyle.CssClass = "";
                ddlMonth.ControlStyle.CssClass = "";
                ddlYear.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlDay.ControlStyle.CssClass = "";
            ddlMonth.ControlStyle.CssClass = "";
            ddlYear.ControlStyle.CssClass = "";
        }
    }

    protected void CheckDateValidation(object source, ServerValidateEventArgs args)
    {
        int d = Convert.ToInt32(ddlDay.SelectedValue);
        int m = Convert.ToInt32(ddlMonth.SelectedValue);
        int y = Convert.ToInt32(ddlYear.SelectedValue);

        bool isvalid = true;
        string _strDate = m.ToString("00") + "/" + d.ToString("00") + "/" + y.ToString("0000") + " 00:00";
        string _strFormat = "MM/dd/yyyy hh:mm";
        DateTime _dtTemp = new DateTime();
        //Response.Write("<script type='text/javascript'>alert('" + _strDate + "');</script>");
        if (DateTime.TryParseExact(_strDate, _strFormat, new System.Globalization.CultureInfo("en-US"), System.Globalization.DateTimeStyles.None, out _dtTemp))
        {
            isvalid = true;
        }
        else
        {
            isvalid = false;
            //((CustomValidator)source).ErrorMessage = "Year, Month, and Day parameters describe an un-representable DateTime.";
        }

        //((CustomValidator)source).ErrorMessage = "";
        //((CustomValidator)source).Text = "";
        //if (y == -1 || m == -1 || d == -1)
        //{
        //    isvalid = false;
        //}
        //else
        //{
        //    if (y % 4 == 0 || y % 2000 == 0 || y % 400 == 0)
        //    {
        //        if (m == 2 && d > 29)
        //        {
        //            ((CustomValidator)source).ErrorMessage = "";
        //            ((CustomValidator)source).Text = "Maximum days for this month are 29 days!";
        //            isvalid = false;
        //        }
        //    }
        //    else if (m == 2)
        //    {
        //        if (d > 28)
        //        {
        //            ((CustomValidator)source).ErrorMessage = "";
        //            ((CustomValidator)source).Text = "Maximum days for this month are 28 days!";
        //            isvalid = false;
        //        }
        //    }

        //    if (m == 4 || m == 6 || m == 9 || m == 11)
        //    {
        //        if (d > 30)
        //        {
        //            isvalid = false;
        //            ((CustomValidator)source).ErrorMessage = "";
        //            ((CustomValidator)source).Text = "Maximum days for this month are 30 days!";
        //        }
        //    }

        //    if (isvalid == true && bAllowGreaterThanToday == false)
        //    {
        //        DateTime dTemp = new DateTime(y, m, d);
        //        // Less than Zero - This instance is earlier than value. 
        //        // Equal - This instance is the same as value. 
        //        //This instance is later than value. 
        //        if (dTemp.Date.CompareTo(General.GetUTCDateTime().Date) > 0)
        //        {
        //            isvalid = false;
        //            ((CustomValidator)source).ErrorMessage = "Selected date is later than today date.!";
        //        }
        //    }
        //}
        args.IsValid = isvalid;
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }


    public bool IsAllowGreaterThanToday
    {
        get
        {
            return bAllowGreaterThanToday;
        }
        set
        {
            bAllowGreaterThanToday = value;
            txtAllowGreaterThanToday.Text = bAllowGreaterThanToday == true ? "1" : "0";
        }
    }

    public bool IsTravelInsurance
    {
        get { return bIsTravelInsurance; }
        set
        {
            bIsTravelInsurance = value;
            txtIsTravelInsurance.Text = bIsTravelInsurance == true ? "1" : "0";
        }
    }

    public bool IsNotLessThanToday
    {
        get { return bIsNotLessThanToday; }
        set
        {
            bIsNotLessThanToday = value;
            txtIsNotLessThanToday.Text = bIsNotLessThanToday == true ? "1" : "0";
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        csvDate.Enabled = bValue;
        rfvDay.Enabled = bValue;
        rfvMonth.Enabled = bValue;
        rfvYear.Enabled = bValue;
    }

    public Int32 Day
    {
        get { return intDay; }
        set { intDay = value; }
    }

    public Int32 Month
    {
        get { return intMonth; }
        set { intMonth = value; }
    }

    public Int32 Year
    {
        get { return intYear; }
        set { intYear = value; }
    }

    public Int32 StartYear
    {
        get { return intStartYear; }
        set { intStartYear = value; }
    }

    public Int32 EndYear
    {
        get { return intEndYear; }
        set { intEndYear = value; }
    }

    public string ValidationGroup
    {
        get { return strValidationGroup; }
        set { strValidationGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        txtAllowGreaterThanToday.Text = bAllowGreaterThanToday == true ? "1" : "0";

        //if (intStartYear == 0) intStartYear = General.GetUTCDateTime().Year - 75;
        //if (intEndYear == 0) intEndYear = General.GetUTCDateTime().Year + 20;

        //ddlYear.Items.Clear();

        //for (int i = intEndYear; i >= intStartYear; i--)
        //{
        //    ddlYear.Items.Add(i.ToString());
        //}

        //ddlYear.Items.Insert(0, new ListItem("Year", "-1"));

        if ((intDay > 0 && intDay < 32)
            && (intMonth > 0 && intMonth < 13)
            && (intYear >= intStartYear && intYear <= intEndYear))
        {
            ddlDay.Text = intDay.ToString();
            ddlMonth.Text = intMonth.ToString();
            ddlYear.Text = intYear.ToString();
        }
        else
        {
            //ddlDay.Text = General.GetUTCDateTime().Day.ToString();
            //ddlMonth.Text = General.GetUTCDateTime().Month.ToString();
            //ddlYear.Text = General.GetUTCDateTime().Year.ToString();
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strValidationGroup != string.Empty)
        {
            csvDate.ValidationGroup = strValidationGroup;
            rfvDay.ValidationGroup = strValidationGroup;
            rfvMonth.ValidationGroup = strValidationGroup;
            rfvYear.ValidationGroup = strValidationGroup;
        }

        //if (strValidationGroup != string.Empty)
        //    csvMonth.ValidationGroup = strValidationGroup;

        //if (strValidationGroup != string.Empty)
        //    csvYear.ValidationGroup = strValidationGroup;

        //if (strErrorMessage != string.Empty)
        //{
        //    csvDate.ErrorMessage = csvDate.ClientID.ToString() + " " + strErrorMessage;
        //}
        //csvDate.ErrorMessage = csvDate.ClientID.ToString();

        //if (strMonthErrorMessage != string.Empty)
        //{
        //    csvMonth.ErrorMessage = strMonthErrorMessage;
        //}

        //if (strYearErrorMessage != string.Empty)
        //{
        //    csvYear.ErrorMessage = strYearErrorMessage;
        //}
    }

    private void SetEnabled()
    {
        //ddlDay.SelectedIndex = 0;
        //ddlMonth.SelectedIndex = 0;
        //ddlYear.SelectedIndex = 0;

        ddlDay.Enabled = bEnabled;
        ddlMonth.Enabled = bEnabled;
        ddlYear.Enabled = bEnabled;

        csvDate.Enabled = bEnabled;
        rfvDay.Enabled = bEnabled;
        rfvMonth.Enabled = bEnabled;
        rfvYear.Enabled = bEnabled;
        //csvMonth.Enabled = bEnabled;
        //csvYear.Enabled = bEnabled;
    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlDay.Text = intDay.ToString();
            ddlMonth.Text = intMonth.ToString();
            ddlYear.Text = intYear.ToString();

            string orgDayText = ddlDay.SelectedItem.Text;
            string orgDayValue = ddlDay.SelectedItem.Value;

            string orgMonthText = ddlMonth.SelectedItem.Text;
            string orgMonthValue = ddlMonth.SelectedItem.Value;

            string orgYearText = ddlYear.SelectedItem.Text;
            string orgYearValue = ddlYear.SelectedItem.Value;

            ddlDay.Items.Clear();
            ddlMonth.Items.Clear();
            ddlYear.Items.Clear();

            ddlDay.Items.Add(new ListItem(orgDayText, orgDayValue));
            ddlMonth.Items.Add(new ListItem(orgMonthText, orgMonthValue));
            ddlYear.Items.Add(new ListItem(orgYearText, orgYearValue));
        }
    }

    private void SetProperty()
    {
        intDay = Convert.ToInt32(ddlDay.Text);
        intMonth = Convert.ToInt32(ddlMonth.Text);
        intYear = Convert.ToInt32(ddlYear.Text);
    }

    public DateTime GetDate()
    {
        //CultureInfo ci = new CultureInfo("en-US");
        //CultureInfo ci = CultureInfo.CurrentCulture;
        //DateTime dtReturn = new DateTime(1900,1,1,ci.Calendar);
        DateTime dtReturn = new DateTime(1900, 1, 1);
        csvDate.Validate();
        if (csvDate.IsValid)// && csvMonth.IsValid && csvYear.IsValid)
        {
            intDay = Convert.ToInt32(ddlDay.Text);

            intMonth = Convert.ToInt32(ddlMonth.Text);
            intYear = Convert.ToInt32(ddlYear.Text);
            if (intDay > 0 && intMonth > 0 && intYear > 0)
            {
                //dtReturn = new DateTime(intYear, intMonth, intDay,ci.Calendar);
                dtReturn = new DateTime(intYear, intMonth, intDay);
            }
        }
        return dtReturn;
    }

}
